package com.example.canatest.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.ad.feign.AdFeign;
import com.changgou.pojo.Ad;
import com.xpand.starter.canal.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.xml.transform.Source;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @author chen.qian
 * @date 2018/3/19
 */
@CanalEventListener
public class MyEventListener {
    @Autowired
    private AdFeign adFeign;

    @Autowired
    private RedisTemplate stringRedisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

/*    @InsertListenPoint
    public void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
    }*/

    @UpdateListenPoint(destination = "example", schema = "changgou_goods", table = {"tb_spu"})
    public void onEvent1(CanalEntry.RowData rowData) {
        System.err.println("UpdateListenPoint");
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        afterColumnsList.forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));

        String afterIsMarketable = afterColumnsList.stream().filter(c -> "is_marketable".equalsIgnoreCase(c.getName()))
                .map(c -> c.getValue()).collect(toList()).get(0);
        System.err.println("afterIsMarketable = " + afterIsMarketable);
        String beforeIsMarketable = beforeColumnsList.stream().filter(c -> "is_marketable".equalsIgnoreCase(c.getName()))
                .map(c -> c.getValue()).collect(toList()).get(0);
        System.err.println("beforeIsMarketable = " + beforeIsMarketable);

        String id = beforeColumnsList.stream().filter(c -> "id".equalsIgnoreCase(c.getName()))
                .map(c -> c.getValue()).collect(toList()).get(0);
        System.err.println("id = " + id);

        if ("0".equals(beforeIsMarketable) && "1".equals(afterIsMarketable)) {
            System.out.println("上架了");
            rabbitTemplate.convertAndSend("changgouExchange01", "IsMarketable.queue.OK", id);
        }
    }

/*    @DeleteListenPoint
    public void onEvent3(CanalEntry.EventType eventType) {
        System.err.println("DeleteListenPoint");*/

    /**
     By--Annotation: id ::   7
     By--Annotation: name ::   qqqq
     By--Annotation: position ::   web_index_lb
     By--Annotation: start_time ::   2019-06-28 09:01:43
     By--Annotation: end_time ::   2019-12-06 07:29:59
     By--Annotation: status ::   1
     By--Annotation: image ::   img/ad1.jpg
     By--Annotation: url ::   ://www.badu.com
     By--Annotation: remarks ::
     */
    @ListenPoint(destination = "example", schema = "changgou_business", table = {"tb_ad"}, eventType = {CanalEntry.EventType.UPDATE,CanalEntry.EventType.INSERT,CanalEntry.EventType.DELETE})
    public void onEvent4(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.err.println("更新redis");
        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
        //web_index_lb
        String position = rowData.getAfterColumnsList().stream()
                .filter(column -> "position".equalsIgnoreCase(column.getName()))
                .map(column -> column.getValue())
                .limit(1).collect(toList()).get(0);

        //远程调用运营微服务
        List<Ad> adList = adFeign.findAdListByposition(position);

        //将Mysql数据中的广告更新到缓存中
        //stringRedisTemplate.delete("ad_" + position);
        stringRedisTemplate.boundValueOps("ad_" + position).set(JSON.toJSONString(adList));
    }
}
